package com.sykj.wangkun.shiro;


import com.sykj.exception.BizException;
import com.sykj.util.reponse.CommonEnum;
import com.sykj.util.reponse.ResultBody;
import com.sykj.wangkun.bean.AdmUser;

import com.sykj.wangkun.service.SysRoleService;
import com.sykj.wangkun.service.SysUserService;
import io.jsonwebtoken.Jwts;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.Map;



/**
 * @author 王坤
 * @className MyRealm
 * @description TODO
 * @Date 2020/5/26 8:43
 */

/**
 * MyRealm：它充当了shiro与应用数据简单桥梁或者可以说是连接器，当用
 * 户执行认证和授权时，shiro会从应用配置中realm中查找用户和权限
 */
public class MyRealm extends AuthorizingRealm {

@Autowired
private SysUserService sysUserService;

@Autowired
private SysRoleService sysRoleService;


    @Override
    public boolean supports(AuthenticationToken token){
        return token instanceof UsernamePasswordToken;
    }
    //权限校验：(验证用户是否有权限)
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        System.out.println("----------------权限校验");
//         AdmUser =(AdmUser) principalCollection.getPrimaryPrincipal();
//        //调用服务层查询用户所有的角色
//        List<Role> queryor = sysRoleService.getById();
//
//        Set<String> roleset=new HashSet<>();
//        Set<String> permission=new HashSet<>();
//
//        for (Role role:queryor){
//            roleset.add(role.getRoleName());
//            System.out.println("角色是："+role.getRoleName());
//            //调用方法查询所有权限
//            List<SysMenu> sysMenus = permissionsService.querype(role.getRid());
//            for (SysMenu sysmenu:sysMenus){
//                permission.add(sysmenu.getUrl());
//            }
//        }
//        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//        info.addRoles(roleset);
//        info.addStringPermissions(permission);
        return null;
    }


    //身份认证：(验证用户输入的账号和密码是否正确)
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("-------------------------------身份验证");
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
        String account= token.getUsername();
        String password=new String(token.getPassword()) ;

        ResultBody resultBody = sysUserService.queryByAccount(account);
        AdmUser admUser =(AdmUser) resultBody.getResult();
        if(admUser!=null){
            //得到加密的盐值
            String salt = admUser.getSalt();
            System.out.println("盐是："+salt);
            //得到加密的密码
            String password1 = admUser.getPassword();
            System.out.println("加密密码是："+password1);

            Map map1 = Jwts.parser().setSigningKey(salt).parseClaimsJws(password1).getBody();
            System.out.println("------------"+map1);

            System.out.println("解密后的密码是："+map1.get("password"));

            String password2=(String) map1.get("password");
//            if(!password.equals(password2)){
//                throw   new BizException(CommonEnum.NOT_FOUND);
//            }

        }
        else{
            throw new AuthenticationException("密码错误");
        }



        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(account,password,getName());
        return info;
    }


}
